package com.ruoyi.lowcode.controller;

import com.ruoyi.lowcode.service.VoteService;
import com.ruoyi.lowcode.vo.R;
import com.ruoyi.lowcode.vo.VoteEchartVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/lowcode/vote")
@Slf4j
public class VoteController {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    VoteService voteService;

    private String prefix = "lowcode/vote";
    @GetMapping()
    public String vote()
    {
        return prefix + "/vote";
    }

    @GetMapping("/votelist")
    @ResponseBody
    public R votelist() throws Exception {
        VoteEchartVo voteEchartVo =  voteService.voteListForEchart();
        return R.ok(voteEchartVo);
    }

    @ResponseBody
    @GetMapping("/dovote/{phone}/{name}")
    public R doVote(@PathVariable("phone") String phone, @PathVariable("name") String name) throws Exception {
        log.info("入参为:phone={},name={}",phone,name);

        // 检查参数是否为空，如果为空则直接抛出异常
        if (phone==null || phone.equals("")){
            throw new Exception("手机号为空!");
        }

        if (name==null || name.equals("")){
            throw new Exception("候选人名字为空!");
        }

        // 调用service对象进行具体的投票业务逻辑处理
        HashMap<String, Object> data = voteService.doVote(phone, name);

        // 返回结果
        return R.ok(data);
    }

}
